<?php
//namespace student\controller\welcome;
defined('BASEPATH') OR exit('No direct script access allowed');

class Userinfo extends CI_Controller {


	public function __construct()
	{
		parent::__construct();
		//$this->load->model('');
		$this->load->helper('url_helper');
		$this->load->model("getuser");
		session_start();
	}

    
    /**
    ** 用户信息主函数
    **/
	public function index()
	{

		//$this->load->view('welcome_message');

		$tel = $_SESSION['user_name'];
        /*if(file_exists(base_url()."picture/user/".$_SESSION['user_id'].".jpg")){
            var_dump(file_exists(base_url()."picture/user/".$_SESSION['user_id'].".jpg"));exit;
        }*/
        $data = $this->getuser->getUserInfo($tel);
        $res_data = array('data'=>$data);
		$this->load->view('home/userinfo/top_userinfo');
		$this->load->view('home/userinfo/userinfo/userinfo',$res_data);
		$this->load->view('home/bottom_detaildata');
	}


    /**
    ** 修改用户名
    **/
    public function changeUserName()
    {
    	$newName = $_REQUEST['newName'];

    	$res = $this->getuser->changeUserName($newName);
    	if($res){
    		print json_encode(array('code'=>0,'data'=>$newName,'desc'=>'修改成功'));
    	} else {
    		print json_encode(array('code'=>1,'desc'=>'修改用户名失败'));
    	}
    }


    /**
    *  用户安全设置页面展示
    **/
    public function safetysetting()
    {
        $type = @$_REQUEST['type']?@$_REQUEST['type']:"show";
        $data = array('type'=>$type);
        $this->load->view("home/userinfo/top_userinfo");
        $this->load->view("home/userinfo/userinfo/safety",$data);
        $this->load->view("home/bottom_detaildata");
    }


    /**
    ** 修改用户密码
    **/
    public function changePassword()
    {
        $new_pass = $_REQUEST['password'];
        $user_id = $_SESSION['user_id'];
        $oldpass = $_REQUEST['oldpassword'];
        $res =$this->getuser->verifyLogin($_SESSION['user_name'],$oldpass);
        if(empty($res)){
            print json_encode(array("code"=>3,"desc"=>"原密码输入错误"));return 0;
        }
        $res = $this->getuser->changePass($new_pass);
        if($res){
            print json_encode(array('code'=>1,'desc'=>"修改密码成功"));
        } else {
            print json_encode(array('code'=>2,'desc'=>'修改密码失败'));
        }
    }
}
